import space  from '../namespaces'
var myspace = space.permission;
var request = require("superagent");

const actions = {

  [myspace + '/initAdd']({commit, state, rootState}, arg){
    commit(myspace + '/initAdd');
    initCheckbox({commit, state, rootState}, arg);
  },
  [myspace + '/initEdit']({commit, state, rootState}, arg){
    initCheckbox({commit, state, rootState}, arg);
    initEdit({commit, state, rootState}, arg);
  },
  //check事件
  [myspace + '/checkme']({commit, state, rootState}, arg){
    commit(myspace + '/checkme', {arg: arg});
  },
  [myspace + '/checkall']({commit, state, rootState}, arg){
    commit(myspace + '/checkall', {arg: arg});
  },
  //submit事件
  [myspace + '/submitme']({commit, state, rootState}, arg){
    return new Promise((resolve)=> {
      submitme({commit, state, rootState}, arg, resolve);
    });

  },
  [myspace + '/initOperate']({commit, state, rootState}, arg){


    return new Promise((resolve)=> {
      initOperate({commit, state, rootState}, arg, resolve);
    });


  }

}


function initCheckbox({commit, state, rootState}, arg) {

  var query = {}
  if (arg) {
    query = {id: arg.id}
  }

  request
    .get('/user/ControlGroup/getSortControl?skipme=1')
    .query(query)
    .end(function (err, res) {
      if (res.ok && res.body.code == '00000') {
        commit(myspace + '/initChekbox', {list: res.body.data});
      }
    });

}


function submitme({commit, state, rootState}, arg, resolve) {

    let url = `/user/control_group/${arg.type}`

    let postData = {name: arg.name, depict: arg.depict}


    if (arg.type == 'update') {
	postData.id = arg.id
    }
    //checkbox的值 controls
    let controls = [];

    state.loopData.forEach(function(val,key){
        if(val.check){
            controls.push({id:val.id,value:val.value,parent_id:val.parent_id,level:val.level,id:val.id})
        }
    });

    if (controls) {
	postData.controls = controls
    }

    request
	.post(url)
	.send(postData)
	.end(function (err, res) {

	    if (res.body.code == '00000') {
		commit(myspace + '/submitme');
		resolve('ok');
	    }
	    resolve(res.body.msg);
	});


}


function initEdit({commit, state, rootState}, arg) {

  request
    .get('/user/control_group/one')
    .query({id: arg.id, skipme: 1})
    .end(function (err, res) {

      if (res.ok && res.body.code == '00000') {
        let arg = {
          name: res.body.data.name,
          depict: res.body.data.depict
        }
        commit(myspace + '/update_control_group', arg);
      }
    });


}

//更新操作选项
function initOperate({commit, state, rootState}, arg, resolve) {
  let ctl = rootState.current_ctl


  if (state[ctl + '_init']) {
    resolve();
    return;
  }

  state[ctl + '_init'] = true;

  request
    .get('/user/limit/getOperates')
    .query({ctl: ctl})
    .end(function (err, res) {

      if (res.ok && res.body.code == '00000') {

        let arg = {
          ctl: ctl,
          list: res.body.data,
          rootState: rootState
        }

        commit(myspace + '/initOperate', arg);
        resolve();
      }
    });


}

export default actions;
